... | ... | @@ -48,16 +48,32 @@ Créez un fichier properties qui va reprendre l'ensemble de ses informations, pl |
|
|
Par exemple, `src\main\resources\spring\database.properties` :
|
|
|
|
|
|
```properties
|
|
|
# Exemple pour Hibernate 4
|
|
|
db.driver=com.mysql.cj.jdbc.Driver
|
|
|
db.url=jdbc:mysql://localhost/banque?useSSL=false&serverTimezone=Europe/Paris
|
|
|
db.url=jdbc:mysql://localhost/banque?useSSL=false&serverTimezone=Europe/Paris&allowPublicKeyRetrieval=true
|
|
|
db.login=root
|
|
|
db.password=
|
|
|
hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
|
|
|
|
|
|
hibernate.connection.pool_size=20
|
|
|
hibernate.cache.provider_class=org.hibernate.cache.internal.NoCacheProvider
|
|
|
hibernate.cache.use_second_level_cache=false
|
|
|
```
|
|
|
|
|
|
```properties
|
|
|
# Exemple pour Hibernate 5
|
|
|
db.driver=com.mysql.cj.jdbc.Driver
|
|
|
db.url=jdbc:mysql://localhost/banque?useSSL=false&serverTimezone=Europe/Paris&allowPublicKeyRetrieval=true
|
|
|
db.login=root
|
|
|
db.password=
|
|
|
hibernate.dialect.storage_engine=innodb
|
|
|
|
|
|
# https://docs.jboss.org/hibernate/orm/current/userguide/html_single/Hibernate_User_Guide.html#configurations
|
|
|
hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
|
|
|
hibernate.connection.pool_size=20
|
|
|
hibernate.cache.provider_class=org.hibernate.cache.NoCacheProvider
|
|
|
hibernate.cache.use_second_level_cache=false
|
|
|
hibernate.show_sql=false
|
|
|
hibernate.connection.handling_mode=DELAYED_ACQUISITION_AND_HOLD
|
|
|
```
|
|
|
|
|
|
Vous pouvez changer les clefs si vous le désirez.
|
... | ... | @@ -177,10 +193,12 @@ En Hibernate 5, la [sessionFactory](https://docs.spring.io/spring-framework/docs |
|
|
</property>
|
|
|
<property name="hibernateProperties">
|
|
|
<props>
|
|
|
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
|
|
|
<prop key="hibernate.dialect.storage_engine">${hibernate.dialect.storage_engine}</prop>
|
|
|
<prop key="hibernate.connection.handling_mode">${hibernate.connection.handling_mode}</prop>
|
|
|
<prop key="hibernate.connection.pool_size">${hibernate.connection.pool_size}</prop>
|
|
|
<prop key="hibernate.cache.provider_class">${hibernate.cache.provider_class}</prop>
|
|
|
<prop key="hibernate.cache.use_second_level_cache">${hibernate.cache.use_second_level_cache}</prop>
|
|
|
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
|
|
|
</props>
|
|
|
</property>
|
|
|
</bean>
|
... | ... | @@ -225,23 +243,29 @@ En Hibernate 4, la [sessionFactory](https://docs.spring.io/spring-framework/docs |
|
|
En Hibernate 5, la [sessionFactory](https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/orm/hibernate5/LocalSessionFactoryBean.html) ressemblera à :
|
|
|
|
|
|
```java
|
|
|
@Bean
|
|
|
public org.springframework.orm.hibernate5.LocalSessionFactoryBean sessionFactory(
|
|
|
DataSource dataSource,
|
|
|
@Value("${hibernate.dialect}") String dialect,
|
|
|
@Value("${hibernate.connection.pool_size}") Integer pSize,
|
|
|
@Value("${hibernate.cache.provider_class}") String pCacheProviderClass,
|
|
|
@Value("${hibernate.cache.use_second_level_cache}") Boolean pUseSecondLevelCache) {
|
|
|
@Autowired
|
|
|
private Environment env;
|
|
|
|
|
|
@Bean
|
|
|
public org.springframework.orm.hibernate5.LocalSessionFactoryBean sessionFactory() {
|
|
|
// LocalSessionFactoryBean est un factoryBean qui produit des sessionFactory
|
|
|
org.springframework.orm.hibernate5.LocalSessionFactoryBean resu = new org.springframework.orm.hibernate5.LocalSessionFactoryBean();
|
|
|
resu.setDataSource(dataSource);
|
|
|
resu.setMappingResources("hibernate/compte.hbm.xml", "hibernate/operation.hbm.xml","hibernate/utilisateur.hbm.xml");
|
|
|
resu.setDataSource(this.dataSource());
|
|
|
resu.setMappingResources("hibernate/compte.hbm.xml", "hibernate/operation.hbm.xml",
|
|
|
"hibernate/utilisateur.hbm.xml");
|
|
|
Properties hibernateProperties = new Properties();
|
|
|
hibernateProperties.put("hibernate.dialect", dialect);
|
|
|
hibernateProperties.put("hibernate.connection.pool_size", pSize);
|
|
|
hibernateProperties.put("hibernate.cache.provider_class", pCacheProviderClass);
|
|
|
hibernateProperties.put("hibernate.cache.use_second_level_cache", pUseSecondLevelCache);
|
|
|
hibernateProperties.put("hibernate.hibernate.show_sql", Boolean.FALSE);
|
|
|
hibernateProperties.put("hibernate.dialect.storage_engine",
|
|
|
this.env.getProperty("hibernate.dialect.storage_engine"));
|
|
|
hibernateProperties.put("hibernate.connection.pool_size",
|
|
|
this.env.getProperty("hibernate.connection.pool_size"));
|
|
|
hibernateProperties.put("hibernate.cache.provider_class",
|
|
|
this.env.getProperty("hibernate.cache.provider_class"));
|
|
|
hibernateProperties.put("hibernate.cache.use_second_level_cache",
|
|
|
this.env.getProperty("hibernate.cache.use_second_level_cache"));
|
|
|
hibernateProperties.put("hibernate.connection.handling_mode",
|
|
|
this.env.getProperty("hibernate.connection.handling_mode"));
|
|
|
// Option qui ne sert à rien, gestion dans log4j
|
|
|
hibernateProperties.put("hibernate.show_sql", this.env.getProperty("hibernate.show_sql"));
|
|
|
resu.setHibernateProperties(hibernateProperties);
|
|
|
return resu;
|
|
|
}
|
... | ... | |